/**
 * 员工添加js
 */
var comValidform=null;
var returnStatus=false;
var employeeAdd={
	dataCache:null,
	
	bindChangeDept:function(val){
		var url = 'common/ComDutiesCtrl/getDutiesList';
		var params={
				'deptId':val
		}
		var cb = function(data){
			if(data.resultCode==200){
				$('.com-addForm select[name="dutiesId"]').empty();
				var option='<option value="">请选择</option>';
				for(var i=0;i<data.result.length;i++){
					var singleObj=data.result[i];
					option=option+'<option value="'+singleObj.id+'">'+singleObj.name+'</option>';
				}
				$('.com-addForm select[name="dutiesId"]').append(option);
			}
		};
		//sendApiNoLoad(url, params, cb, 'POST');
	},
		
	startEvent:function(){
		//获取部门
		var url = 'common/ComDepartmentCtrl/getDepartmentList';
		var cb = function(data){
			if(data.resultCode==200){
				var option='<option value="">请选择</option>';
				for(var i=0;i<data.result.length;i++){
					var singleObj=data.result[i];
					option=option+'<option value="'+singleObj.id+'">'+singleObj.name+'</option>';
				}
				$('.com-addForm select[name="deptId"]').append(option);
				$('.com-addForm select[name="deptId"]').on('click',function(){
					employeeAdd.bindChangeDept($(this).val());
				});
			}
		};
		sendApiNoLoad(url, {}, cb, 'POST');
		
		comValidform=$(".com-addForm").Validform({
			tiptype:2,
			tipSweep:true,
			beforeSubmit:function(curform){
				var checkedId='';
				$('.com-addForm').find('input:checkbox[name="roleList"]:checked').each(function(){
					var checkedVal=$(this).val();
					checkedId+=checkedVal.split(':')[0]+',';
				});
				if(checkedId!='')
					checkedId=checkedId.substring(0,checkedId.length-1);
				$('.com-addForm input[name="sysRoleIds"]').val(checkedId);
				
				var url = 'mgr/SysUserCtrl/saveSysUser';
				var params = jsUtils.serializeJson($('.com-addForm'));
				delete params.roleList;
				
				var cb = function(data){
					alert(data.msg);
					//成功返回
					if(data.resultCode==200){
						//关闭窗口并刷新数据
						art.dialog.close();
						returnStatus=true;
					}
				};
				sendApiNoLoadSync(url, params, cb, 'POST');
				return false;
			}
		});
		
		//获取所有模块
		var url ='mgr/SysRoleCtrl/getSysRoleList';
		var params={
			'curPage':0,
			'pageSize':9999
		};
		var cb = function(data){
			employeeAdd.dataCache=data.result;
			var modulesCache=employeeAdd.dataCache;
			var tr='';
			for(var i=0;i<modulesCache.length;i++){
				if(i%4==0){
					tr+='<tr style="height: 25px;">';
				}
				var id=modulesCache[i].id;
				var name=modulesCache[i].name;
				tr+='<td><input type="checkbox" name="roleList" value="'+id+':'+name+'" />'+name+'</td>';
				if(i%4==3){
					tr+='</tr>';
				}
			}
			$('#roleTable').append(tr);
		};
		//sendApiNoLoad(url, params, cb, 'POST');
	},
	
	init:function(){
		employeeAdd.startEvent();
	}
}
$(document).ready(function() {
	employeeAdd.init();
});
function submitForm(){
	comValidform.submitForm();
	return returnStatus;
}